package c1725163225.mzxy.androistest.androidtest;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import c1725163225.mzxy.androistest.androidtest.model.BannerModel;

public class MainActivity extends AppCompatActivity {
    ViewPager mViewPager;
    MyViewPager myViewPager;
    LinearLayout mDotLayout;
    ListView mListView;
    MyAdapter myAdapter;
    TextView toptitletv;

    ArrayList<ImageView>imageViews=new ArrayList<>();
    ArrayList<BannerModel.Topstories>bannerlist=new ArrayList<>();
    ArrayList<BannerModel.Date>list=new ArrayList<>();
    Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i("MainActivity"," hand message");
            handler.sendEmptyMessageDelayed(1,2000);
            mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDotLayout=findViewById(R.id.dotlayout);
        mListView=findViewById(R.id.list);
        toptitletv=findViewById(R.id.manweitv);
        myAdapter=new MyAdapter();
        mListView.setAdapter(myAdapter);
        mViewPager=findViewById(R.id.viewPager);

        myViewPager=new MyViewPager();
        mViewPager.setAdapter(myViewPager);


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i==0){
                    mViewPager.setCurrentItem(bannerlist.size()-2,false);
                }
                if (i==bannerlist.size()-1){
                    mViewPager.setCurrentItem(1,false);
                }
                setDots();
                Log.i("MainActivity","MainActivity:"+1);

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        x.Ext.init(getApplication());
        RequestParams requestParams=new RequestParams("https://news-at.zhihu.com/api/4/news/latest");


        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("MainActivity",result);
                Gson gson=new Gson();
                BannerModel bannermodel=gson.fromJson(result,BannerModel.class);
                if(bannermodel!=null && bannermodel.top_stories!=null){
                    bannerlist.addAll(bannermodel.top_stories);

                    bannerlist.add(bannermodel.top_stories.get(0));
                    bannerlist.add(0,bannermodel.top_stories.get(bannermodel.top_stories.size()-1));
                    mViewPager.setAdapter(myViewPager);
                    mViewPager.setCurrentItem(1);
                    Log.i("MainActivity","开始发送");
                    handler.sendEmptyMessageDelayed(1,2000);
                    for(int i=0;i<bannermodel.top_stories.size();i++){
                        ImageView imageView=new ImageView(MainActivity.this);
                        imageView.setBackgroundResource(R.drawable.courseimg);
                        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(40,40);
                        params.leftMargin=10;
                        params.rightMargin=10;
                        params.topMargin=25;
                        imageView.setLayoutParams(params);
                        mDotLayout.addView(imageView);
                        imageViews.add(imageView);
                    }
                    setDots();

                }
                RequestParams requestParams1=new RequestParams("https://news-at.zhihu.com/api/4/news/latest");
                x.http().get(requestParams1, new Callback.CommonCallback<String>() {

                    @Override
                    public void onSuccess(String result) {
                        Log.i("Main",result);
                        Gson gson1=new Gson();
                        BannerModel model=gson1.fromJson(result,BannerModel.class);
                        list=model.stories;
                        mListView.setAdapter(myAdapter);


                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.i("Main",ex.getMessage());

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }


                    public boolean onCache(String result) {
                        return false;
                    }
                });


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("Main",ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }


            public boolean onCache(String result) {
                return false;
            }
        });


    }
    public void setDots(){
        for(int i=0;i<imageViews.size();i++){
            if(i==mViewPager.getCurrentItem()-1){
                imageViews.get(i).setSelected(true);
            }else {
                imageViews.get(i).setSelected(false);
            }
        }
    }
    public class MyViewPager extends PagerAdapter{
        @Override
        public int getCount() {
            return bannerlist.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view==o;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);

        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            ImageView imageView=new ImageView(MainActivity.this);
            x.image().bind(imageView, String.valueOf(bannerlist.get(position).image));
            toptitletv.setText(bannerlist.get(position).title);
            container.addView(imageView);

            return imageView;
        }
    }

    public class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView==null){
                viewHolder=new ViewHolder();
                convertView=View.inflate(MainActivity.this,R.layout.listlayout,null);
                viewHolder.mTextView=convertView.findViewById(R.id.tv1);
                viewHolder.imageView=convertView.findViewById(R.id.img);
                viewHolder.mconstraintLayout=convertView.findViewById(R.id.constraintlayout);
                convertView.setTag(viewHolder);
            }else{
                viewHolder= (ViewHolder) convertView.getTag();


            }
            viewHolder.mTextView.setText(list.get(position).title);
            String[] images=list.get(position).images;
            x.image().bind(viewHolder.imageView, images[0]);
            viewHolder.mconstraintLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,DetailActivity.class);
                    intent.putExtra("id",String.valueOf(list.get(position).id));
                    startActivity(intent);
                }
            });
            return convertView;
        }
        public  class ViewHolder {
            TextView mTextView;
            ImageView imageView;
            ConstraintLayout mconstraintLayout;
        }
    }
}

